/*
 * Copyright (C), 2007-2018, 铭师堂教育科技有限公司
 * FileName: OpsInfoServiceImp.java
 * Author:   zhaoyi
 * Date:     2018年5月24日 上午10:49:38
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.secsbrain.cmdb.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.secsbrain.cmdb.biz.IDeployEngineService;
import com.secsbrain.cmdb.biz.IHostImageOptionService;
import com.secsbrain.cmdb.client.model.DeployEngineDto;
import com.secsbrain.cmdb.dal.model.AppEnv;
import com.secsbrain.cmdb.dal.model.DeployEngine;
import com.secsbrain.cmdb.dal.model.HostImageOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RestController;

import com.secsbrain.cmdb.biz.IAppEnvService;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.cmdb.client.iface.OpsInfoService;

/**
 * 运维相关服务
 *
 * @author zhaoyi
 */
@RestController
@EnableEurekaClient
public class OpsInfoServiceImp implements OpsInfoService{
	
	@Autowired
	IAppEnvService appEnvService;

	@Autowired
	IDeployEngineService deployEngineService;
	
	@Autowired
	CommonService commonService;
	
	@Autowired
    IHostImageOptionService hostImageOptionService;

	/* (non-Javadoc)
	 * @see OpsInfoService#queryEnvList()
	 */
	@Override
	public ResponseVo<List<String>> queryEnvList() {
		List<AppEnv> list=appEnvService.selectList(new AppEnv());
		List<String> envList=new ArrayList<String>();
		for(AppEnv appEnv:list){
			envList.add(appEnv.getName());
		}
		return ResponseVo.getSuccessResponse(envList);
	}

	/**
	 * 功能描述: <br>
	 * 根据环境名查询发布引擎信息
	 *
	 * @param envName
	 */
	@Override
	public ResponseVo<DeployEngineDto> queryDeployEngineByEnv(String envName) {
		DeployEngine deployEngineParam=new DeployEngine();
		deployEngineParam.setEnvName(envName);
		DeployEngine deployEngine = deployEngineService.selectOne(deployEngineParam);
		DeployEngineDto deployEngineDto=new DeployEngineDto();
		deployEngineDto.setDeployUrl(deployEngine.getDeployUrl());
		deployEngineDto.setEnvName(deployEngine.getEnvName());
		deployEngineDto.setId(deployEngine.getId());
		deployEngineDto.setName(deployEngine.getName());
		return ResponseVo.getSuccessResponse(deployEngineDto);
	}

	/* (non-Javadoc)
	 * @see OpsInfoService#queryImageOptionList()
	 */
	@Override
	public ResponseVo<List<String>> queryImageOptionList() {
		List<HostImageOption> list=hostImageOptionService.selectList(new HostImageOption());
		List<String> imageOptionList=new ArrayList<String>();
		for(HostImageOption hostImageOption:list){
			imageOptionList.add(hostImageOption.getName());
		}
		return ResponseVo.getSuccessResponse(imageOptionList);
	}

	/* (non-Javadoc)
	 * @see OpsInfoService#queryEnvListByFlag(java.lang.String)
	 */
	@Override
	public ResponseVo<List<String>> queryEnvListByFlag(String flag) {
		AppEnv appEnvParam=new AppEnv();
		List<AppEnv> appEnvList=appEnvService.selectList(appEnvParam);
		Map<Long, String> idcMap=commonService.getIdcMap();
		List<String> resultList=new ArrayList<String>();
		for(AppEnv appEnv:appEnvList){
			if("MDC".equals(idcMap.get(appEnv.getIdcId()))
					||"SJC".equals(idcMap.get(appEnv.getIdcId()))){
				//线上机房
				if("online".equals(flag)){
					resultList.add(appEnv.getName());
				}
			}else{
				if("offline".equals(flag)){
					resultList.add(appEnv.getName());
				}
			}
		}
		return ResponseVo.getSuccessResponse(resultList);
	}
	
}
